package controllers.letzmeet.common;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import play.Logger;
import play.mvc.After;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Scope;
import service.CommonService;
import service.WebService;

/**
 * <pre>
 * 2012.04.24
 * LetzMeet 최상위 컨트롤러 클래스
 * </pre>
 * @author Bak Sang Heon
 *
 */
public class CommonController extends Controller {
	
	protected static String CODE = "result_code";
	
	protected static String WEB_ERROR_NOPAGE = "/web/errors/nopage.html";
	
	@Before
	public static void before(){
		// TODO
		Logger.debug("Before Controller");
	} // before()
	
	@After
	public static void after(){
		// TODO
		Logger.debug("After Controller");
	} // after()
	
	/**
	 * <pre>
	 * 웹 서비스 객체를 생성하여 반환한다
	 * 2012.05.03 : 신규작성 (박상헌)
	 * </pre>
	 * @param clazz
	 * @return
	 */
	protected static <T extends WebService> T getService(Class clazz) {
		WebService service = null;
		
		try {
			Constructor c = clazz.getConstructor(Http.Request.class, Http.Response.class, Scope.Session.class, Scope.Flash.class);
			service = (WebService)c.newInstance(request, response, session, flash);
		} catch (SecurityException e) {
			Logger.error(e, e.getMessage());
		} catch (NoSuchMethodException e) {
			Logger.error(e, e.getMessage());
		} catch (IllegalArgumentException e) {
			Logger.error(e, e.getMessage());
		} catch (InstantiationException e) {
			Logger.error(e, e.getMessage());
		} catch (IllegalAccessException e) {
			Logger.error(e, e.getMessage());
		} catch (InvocationTargetException e) {
			Logger.error(e, e.getMessage());
		} // try
		
		return (T)service;
	} // getService
} // class CommonController
